package com.huxiu.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huxiu.BaseActivity;
import com.huxiu.R;
import com.huxiu.SubmitCommentActivity;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.model.CommentItem;
import com.huxiu.net.responses.TuiJianDataResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.ErrorResponseException;
import com.huxiu.utils.ImageLoaderOptions;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.huxiu.view.MyListView;
import com.huxiu.view.RoundImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class CommentHolder implements OnClickListener {

    @ViewInject(R.id.container)
    private LinearLayout container;

    @ViewInject(R.id.avatar)
    private RoundImageView avatar;

    @ViewInject(R.id.name)
    private TextView name;

    @ViewInject(R.id.text_disagree)
    private TextView disagree;

    @ViewInject(R.id.text_agree)
    private TextView agree;

    @ViewInject(R.id.content)
    private TextView content;

    @ViewInject(R.id.time)
    private TextView time;

    @ViewInject(R.id.huifu)
    private TextView huifu;

    public Drawable DRAWABLE_AGREE;
    public Drawable DRAWABLE_AGREED;
    public Drawable DRAWABLE_DISAGREE;
    public Drawable DRAWABLE_DISAGREED;

    public List<CommentItem> mlists;
    public List<CommentItem.Recomment> mRecommentsList;
    ;
    public int mposition;

    private static Activity mContext;

    @ViewInject(R.id.commit_list_huifu)
    private MyListView commit_list_huifu;
    private static WeakReference<BaseAdapter> mAdapter;

    public void setAdapter(BaseAdapter adapter) {
        mAdapter = new WeakReference<>(adapter);
    }

    public CommentHolder(View view, Context context) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        huifu.setOnClickListener(this);
        disagree.setOnClickListener(this);
        agree.setOnClickListener(this);
        DRAWABLE_AGREE = mContext.getResources().getDrawable(R.drawable.icon_agree);
        DRAWABLE_AGREED = mContext.getResources().getDrawable(R.drawable.icon_agreed);
        DRAWABLE_DISAGREE = mContext.getResources().getDrawable(R.drawable.icon_disagree);
        DRAWABLE_DISAGREED = mContext.getResources().getDrawable(R.drawable.icon_disagreed);
        DRAWABLE_AGREE.setBounds(0, 0, DRAWABLE_AGREE.getMinimumWidth(), DRAWABLE_AGREE.getMinimumHeight()); //设置边界
        DRAWABLE_AGREED.setBounds(0, 0, DRAWABLE_AGREED.getMinimumWidth(), DRAWABLE_AGREED.getMinimumHeight()); //设置边界
        DRAWABLE_DISAGREE.setBounds(0, 0, DRAWABLE_DISAGREE.getMinimumWidth(), DRAWABLE_DISAGREE.getMinimumHeight()); //设置边界
        DRAWABLE_DISAGREED.setBounds(0, 0, DRAWABLE_DISAGREED.getMinimumWidth(), DRAWABLE_DISAGREED.getMinimumHeight()); //设置边界
    }

    private CommentItem mItem;

    public void bindItem(List<CommentItem> lists, int position) {
        mItem = lists.get(position);
        mlists = lists;
        if (mItem.recomments != null && mItem.recomments.length > 0) {
            Log.e("reply", "000000");
            commit_list_huifu.setVisibility(View.VISIBLE);
            mRecommentsList=new ArrayList<>();
            Collections.addAll(mRecommentsList, mItem.recomments);
            adapter = new ReplyAdapter();
            commit_list_huifu.setAdapter(adapter);
        } else {
            commit_list_huifu.setVisibility(View.GONE);
        }
        mposition = position;
        name.setText(mItem.author);
        disagree.setText(mItem.disagree_num + "");
        agree.setText(mItem.agree_num + "");
        content.setText(mItem.message);
        if (mItem.agree) {
            agree.setCompoundDrawables(DRAWABLE_AGREED, null, null, null);
            disagree.setCompoundDrawables(DRAWABLE_DISAGREE, null, null, null);
        } else if (mItem.disagree) {
            agree.setCompoundDrawables(DRAWABLE_AGREE, null, null, null);
            disagree.setCompoundDrawables(DRAWABLE_DISAGREED, null, null, null);
        } else {
            disagree.setCompoundDrawables(DRAWABLE_DISAGREE, null, null, null);
            agree.setCompoundDrawables(DRAWABLE_AGREE, null, null, null);
        }
        time.setText(Utils.getDateString(mItem.dateline));
        ImageLoader.getInstance().displayImage(
                mItem.avatar + Constants.IMAGE_WEBP, avatar, ImageLoaderOptions.options);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.huifu:
                if (!Settings.isLoggedIn()) {
                    Utils.askLogIn(mContext);
                    return;
                }
                Intent intent = new Intent(mContext, SubmitCommentActivity.class);
                intent.putExtra(Constants.COMMENT_ID_KEY, mItem.cid);
                intent.putExtra("type_article", "add_postcomment");
                intent.putExtra(UmenEvent.COMMENT_INTENT_KEY, UmenEvent.COMMENT_LISTREPLY_COMMENT);
                intent.putExtra(Constants.REPLYNAME, mItem.author);
                mContext.startActivity(intent);
                break;
            case R.id.text_disagree:
                if (!Settings.isLoggedIn()) {
                    Utils.askLogIn(mContext);
                    return;
                }
                parise("disagree");
                break;
            case R.id.text_agree:
                if (!Settings.isLoggedIn()) {
                    Utils.askLogIn(mContext);
                    return;
                }
                parise("agree");
                break;
        }
    }


    //点赞功能
    private void parise(final String agress){
        String url = NetworkConstants.ZAN_URL;
        final Map<String, String> params = new LinkedHashMap<>();
        if (agress.equals("agree")) {
            params.put("act", "agree_comments");
            if (mItem.agree) {
                params.put("id", "-" + mItem.cid);
            } else {
                params.put("id", mItem.cid);
            }
        } else {
            params.put("act", "disagree_comments");
            if (mItem.disagree) {
                params.put("id", "-" + mItem.cid);
            } else {
                params.put("id", mItem.cid);
            }
        }
        GsonRequest<TuiJianDataResponse> request = new GsonRequest<>(url, Request.Method.POST,
                TuiJianDataResponse.class, false, params, new Response.Listener<TuiJianDataResponse>() {
            @Override
            public void onResponse(TuiJianDataResponse response) {
                Log.e("response", "response" + response.success + response.data);
                if (response.isSuccess()) {

                    if (agress.equals("agree")) {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.COMMENT_LIST, UmenEvent.COMMENT_LISTLIKE_COMMENT);
                        if (mItem.agree){
                            agree.setCompoundDrawables(DRAWABLE_AGREE, null, null, null);
                            mlists.get(mposition).agree_num = mlists.get(mposition).agree_num - 1;
                        } else {
                            agree.setCompoundDrawables(DRAWABLE_AGREED, null, null, null);
                            mlists.get(mposition).agree_num = mlists.get(mposition).agree_num + 1;
                        }
                        agree.setText(mlists.get(mposition).agree_num + "");
                        mlists.get(mposition).agree = !mlists.get(mposition).agree;
                    } else {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.COMMENT_LIST, UmenEvent.COMMENT_LISTUNLIKE_COMMENT);
                        if (mItem.disagree){
                            disagree.setCompoundDrawables(DRAWABLE_DISAGREE, null, null, null);
                            mlists.get(mposition).disagree_num = mlists.get(mposition).disagree_num - 1;
                        } else {
                            mlists.get(mposition).disagree_num = mlists.get(mposition).disagree_num + 1;
                            disagree.setCompoundDrawables(DRAWABLE_DISAGREED, null, null, null);
                        }
                        disagree.setText(mlists.get(mposition).disagree_num + "");
                        mlists.get(mposition).disagree = !mlists.get(mposition).disagree;
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Throwable t = error.getCause();
                if (t instanceof ErrorResponseException) {
                    if ("操作失败".equals(error.getMessage())) {
                        if (agress.equals("agree")) {
                            Utils.showToast("您已经赞过了");
                        }else{
                            Utils.showToast("您已经踩过了");
                        }

                        return;
                    }
                    Utils.showToast(error.getMessage());
                    Log.e("params", "message" + error.toString());
                } else {
                    Utils.showToast(R.string.generic_failure);
                }
            }
        });
        BaseActivity.getmQueue().add(request);
        //        mQueue.start();
    }

    ReplyAdapter adapter;

    private class ReplyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mRecommentsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mRecommentsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ReplyHolder holder;
            View view = convertView;
            if (view == null) {
                view = View.inflate(mContext, R.layout.item_reply_list, null);
                holder = new ReplyHolder(view, mContext);
                view.setTag(holder);
            }
            holder = (ReplyHolder) view.getTag();
            holder.setAdapter(adapter);
            holder.bindItem(mRecommentsList.get(position));
            return view;
        }

    }

}
